package com.smartkey.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.project.smartkey.R;
import com.smartkey.bean.AppInfo;
import com.smartkey.sharedpreferences.SharePreferencesUtil;

public class AppUtil {

	/**
	 * 获取手机安装的非系统应用信息
	 */
	public static List<AppInfo> getNonSystemAppInfo(Context context) {
		ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 用来存储获取的应用信息数据

		List<PackageInfo> packages = context.getPackageManager()
				.getInstalledPackages(0);

		for (int i = 0; i < packages.size(); i++) {
			PackageInfo packageInfo = packages.get(i);
			AppInfo tmpInfo = new AppInfo();
			tmpInfo.appName = packageInfo.applicationInfo.loadLabel(
					context.getPackageManager()).toString();
			tmpInfo.packageName = packageInfo.packageName;
			tmpInfo.versionName = packageInfo.versionName;
			tmpInfo.versionCode = packageInfo.versionCode;
			tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(context
					.getPackageManager());
			tmpInfo.print();
			
			// 只显示非系统应用信息
			if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
				appList.add(tmpInfo);// 如果非系统应用，则添加至appList
			}else{
				Log.d("AppUtil", "packageNme:"+tmpInfo.packageName);
			}

		}
		return appList;
	}

	/**
	 * 获取默认的APP列表
	 * 
	 * @param context
	 * @return
	 */
	public static List<AppInfo> getDefaulAppInfo(Context context) {
		ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 用来存储获取的应用信息数据

		AppInfo tmpInfo = new AppInfo();
		tmpInfo.appName = "拍照";
		tmpInfo.packageName = AppInfo.APPID.TAKE_PHOTO.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.isCheck = false;
		tmpInfo.appId = AppInfo.APPID.TAKE_PHOTO;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_snapshot);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "摄像";
		tmpInfo.packageName = AppInfo.APPID.MAKE_VEDIO.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.isDefault = true;
		tmpInfo.appId = AppInfo.APPID.MAKE_VEDIO;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_camera);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "录音";
		tmpInfo.packageName = AppInfo.APPID.RECORD.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.RECORD;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_recording);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "手电筒";
		tmpInfo.packageName = AppInfo.APPID.FLASHLIGHT.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.FLASHLIGHT;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_flashlight);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "截屏";
		tmpInfo.packageName = AppInfo.APPID.SCREENTSHOT.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.SCREENTSHOT;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_screenshot);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "home键";
		tmpInfo.packageName = AppInfo.APPID.HOME.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.HOME;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_home);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "清除内存";
		tmpInfo.packageName = AppInfo.APPID.CLEARMEMOMRY.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.CLEARMEMOMRY;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources()
				.getDrawable(R.drawable.sapp_cm);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "拨号";
		tmpInfo.packageName = AppInfo.APPID.DIAL.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.DIAL;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_dial);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "短信";
		tmpInfo.packageName = AppInfo.APPID.SENDMESSAGE.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.SENDMESSAGE;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources().getDrawable(
				R.drawable.sapp_message);
		appList.add(tmpInfo);

		tmpInfo = new AppInfo();
		tmpInfo.appName = "旋屏";
		tmpInfo.packageName = AppInfo.APPID.ROTATINGSCREEN.getValue();
		tmpInfo.versionName = "1";
		tmpInfo.versionCode = 1;
		tmpInfo.appId = AppInfo.APPID.ROTATINGSCREEN;
		tmpInfo.isDefault = true;
		tmpInfo.appIcon = context.getResources()
				.getDrawable(R.drawable.sapp_rs);
		appList.add(tmpInfo);

		return appList;
	}

	/**
	 * 获取默认和用户添加的全部APP信息列表
	 * 
	 * @param context
	 * @return
	 */
	public static List<AppInfo> getAllCheckAppInfo(Context context) {
		ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 用来存储获取的应用信息数据
		appList.addAll(getDefaulAppInfo(context));// 获取默认的应用
		List<String> allPackageName = getAllPackageName(context);// 获取用户添加的app包名
		AppInfo info = new AppInfo();
		for (String packageName : allPackageName) {
			info = getAppInfo(context, packageName);
			appList.add(info);
		}

		// SharePreferencesUtil util = new SharePreferencesUtil(context);
		// String sigleClickAPP = util.getSingleClickValue();
		// String doubleClickAPP = util.getDoubleClickValue();
		return appList;

	}

	/**
	 * 从sharedpreferences获取用户添加的APP包名
	 */
	public static List<String> getAllPackageName(Context context) {
		SharePreferencesUtil util = new SharePreferencesUtil(context);
		List<String> packageNameList = util.getAllCheckAppPackageName();
		return packageNameList;
	}

	/**
	 * 根据包名获取应用名和应用icon
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static AppInfo getAppInfo(Context context, String packageName) {
		AppInfo info = new AppInfo();
		PackageManager pm = context.getPackageManager();
		try {
			ApplicationInfo packageInfo = pm.getApplicationInfo(packageName,
					PackageManager.GET_META_DATA
							| PackageManager.GET_SHARED_LIBRARY_FILES);
			info.appName = (String) pm.getApplicationLabel(packageInfo);
			info.packageName = packageName;
			info.appIcon = pm.getApplicationIcon(packageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}

	/**
	 * 根据包名打开应用
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean launchAppByPackageName(Context context,
			String packageName, boolean isDefault) {
		SystemAppLauncher.getInstance().init(context);
		if (isDefault) {
			AppInfo.APPID appId = AppInfo.APPID.valueOf(packageName);
			switch (appId) {
			case TAKE_PHOTO:
				SystemAppLauncher.getInstance().launchCamera();
				break;
			case MAKE_VEDIO:
				SystemAppLauncher.getInstance().launchVedio();
				break;
			case RECORD:
				SystemAppLauncher.getInstance().launchRecoding();
				break;
			case FLASHLIGHT:
				SystemAppLauncher.getInstance().launchFlashlight();
				break;
			case SCREENTSHOT:
				SystemAppLauncher.getInstance().launchScreenShot();
				break;
			case HOME:
				SystemAppLauncher.getInstance().launcherHome();
				break;
			case CLEARMEMOMRY:
				SystemAppLauncher.getInstance().launchClearMemory();
				break;
			case DIAL:
				SystemAppLauncher.getInstance().launchDial();
				break;
			case SENDMESSAGE:
				SystemAppLauncher.getInstance().launchSendMessage();
				break;
			case ROTATINGSCREEN:
				SystemAppLauncher.getInstance().launchRotatingScreen();
				break;
			}
		} else {
			PackageManager packageManager = context.getPackageManager();
			Intent intent = new Intent();
			intent = packageManager.getLaunchIntentForPackage(packageName);
			if (intent != null) {
				context.startActivity(intent);
			} else {
				Toast.makeText(context, "找不到该应用，请重新选择", Toast.LENGTH_SHORT)
						.show();
				;
			}
		}
		return false;
	}

	/**
	 * 获取默认app列表并设置选中状态
	 */
	public static List<AppInfo> getDefaultAppListWithCheckStatus(
			Context context, ClickType clickType) {
		List<AppInfo> defaultApps = getDefaulAppInfo(context);
		/*
		 * SharePreferencesUtil preferencesUtil = new
		 * SharePreferencesUtil(context); String checkedAppPackageName = null;
		 * switch (clickType) { case CLICK: checkedAppPackageName =
		 * preferencesUtil.getSingleClickValue(); break; case DCLICK:
		 * checkedAppPackageName = preferencesUtil.getDoubleClickValue(); break;
		 * case TCLICK: checkedAppPackageName =
		 * preferencesUtil.getTrifectaClickValue(); break; case UNKOWN: return
		 * defaultApps; default: break; } if (null != checkedAppPackageName &&
		 * !"".equals(checkedAppPackageName)) { for (AppInfo appInfo :
		 * defaultApps) { if (appInfo.packageName.equals(checkedAppPackageName))
		 * { appInfo.isCheck = true; break; } } }
		 */
		return defaultApps;
	}

	public static void resumeSelectedApp(Context context,
			SharePreferencesUtil preferencesUtil, ImageView click,
			ImageView dclick, ImageView tclick, TextView txtClick,
			TextView txtDClick, TextView txtTClick) {

		PackageUtils packageUtils = PackageUtils.getInstance(context);

		String clickPackageName = preferencesUtil.getSingleClickValue();
		String dClickPackageName = preferencesUtil.getDoubleClickValue();
		String tClickPackageName = preferencesUtil.getTrifectaClickValue();

		boolean isDefaultClick = preferencesUtil.isDefaultSingle();
		boolean isDefaultDClick = preferencesUtil.isDefaultDouble();
		boolean isDefaultTClick = preferencesUtil.isDefaulTrifecta();

		String packageNameClick = null;
		String packageNameDClick = null;
		String pakcageNameTClick = null;

		Drawable iconClick = null;
		Drawable iconDClick = null;
		Drawable iconTClick = null;

		List<AppInfo> appInfos = AppUtil.getDefaultAppListWithCheckStatus(
				context, ClickType.UNKOWN);
		for (AppInfo appInfo : appInfos) {
			if (isDefaultClick && clickPackageName.equals(appInfo.packageName)) {
				packageNameClick = appInfo.appName;
				iconClick = appInfo.appIcon;
			}
			if (isDefaultDClick
					&& dClickPackageName.equals(appInfo.packageName)) {
				packageNameDClick = appInfo.appName;
				iconDClick = appInfo.appIcon;
			}
			if (isDefaultTClick
					&& tClickPackageName.equals(appInfo.packageName)) {
				pakcageNameTClick = appInfo.appName;
				iconTClick = appInfo.appIcon;
			}
		}

		if (isDefaultClick) {
			click.setBackgroundDrawable(iconClick);
			txtClick.setText(packageNameClick);
		} else {
			PackageInfo packageInfo = packageUtils
					.getPackageInfo(clickPackageName);
			click.setBackgroundDrawable(packageInfo.applicationInfo
					.loadIcon(context.getPackageManager()));
			txtClick.setText(packageInfo.applicationInfo.loadLabel(
					context.getPackageManager()).toString());
		}

		if (isDefaultDClick) {
			dclick.setBackgroundDrawable(iconDClick);
			txtDClick.setText(packageNameDClick);
		} else {
			PackageInfo packageInfo = packageUtils
					.getPackageInfo(dClickPackageName);
			dclick.setBackgroundDrawable(packageInfo.applicationInfo
					.loadIcon(context.getPackageManager()));
			txtDClick.setText(packageInfo.applicationInfo.loadLabel(
					context.getPackageManager()).toString());
		}

		if (isDefaultTClick) {
			tclick.setBackgroundDrawable(iconTClick);
			txtTClick.setText(pakcageNameTClick);
		} else {
			PackageInfo packageInfo = packageUtils
					.getPackageInfo(tClickPackageName);
			tclick.setBackgroundDrawable(packageInfo.applicationInfo
					.loadIcon(context.getPackageManager()));
			txtTClick.setText(packageInfo.applicationInfo.loadLabel(
					context.getPackageManager()).toString());
		}

	}

	public static void setCheckedApp(Context context, AppInfo appInfo,
			ClickType clickType) {
		SharePreferencesUtil sharePreferencesUtil = new SharePreferencesUtil(
				context);
		switch (clickType) {
		case CLICK:
			sharePreferencesUtil.setSingleClickValue(appInfo);
			break;
		case DCLICK:
			sharePreferencesUtil.setDoubleClickValue(appInfo);
			break;
		case TCLICK:
			sharePreferencesUtil.setTrifecta(appInfo);
			break;
		}
	}
}
